import unittest
from collections import defaultdict
from typing import List


class DemoClass:
    pass  # 这里保持空实现，因为它不是被测类


class Solution:
    def judgePoint24(self, cards: List[int]) -> bool:
        c = defaultdict(int)
        self.exp = []
        operates = [lambda x, y: x + y, lambda x, y: x - y, lambda x, y: x * y, lambda x, y: x / y]
        op = "+-*/"
        for card in cards:
            c[card] += 1

        def remove(key):
            c[key] -= 1
            if c[key] == 0:
                del c[key]

        def dfs():
            if len(c) == 1 and tuple(c.values())[0] == 1:
                return abs(tuple(c.keys())[0] - 24) <= 1e-4

            # 选两个
            for card1 in list(c.keys())[:]:
                if c[card1] <= 0: continue
                remove(card1)
                self.exp.append(card1)
                for card2 in list(c.keys())[:]:
                    if c[card2] <= 0: continue
                    remove(card2)
                    self.exp.append(card2)
                    for operate_id in range(len(operates)):
                        if card2 == 0 and operate_id == 3: continue
                        self.exp.append(op[operate_id])
                        result = operates[operate_id](card1, card2)
                        c[result] += 1
                        if dfs():
                            return True
                        remove(result)
                        self.exp.pop()
                    c[card2] += 1
                    self.exp.pop()
                c[card1] += 1
                self.exp.pop()
            return False

        return dfs()


class TestJudgePoint24(unittest.TestCase):
    def setUp(self):
        self.solution = Solution()

    def test_can_reach_24_with_valid_combination(self):
        # 测试可以组成24的情况
        self.assertTrue(self.solution.judgePoint24([4, 1, 8, 7]))  # 例如 (8-4)*(7-1)=24

    def test_cannot_reach_24(self):
        # 测试无法组成24的情况
        self.assertFalse(self.solution.judgePoint24([1, 2, 1, 2]))

    def test_floating_point_calculation(self):
        # 测试需要浮点数计算的情况
        self.assertTrue(self.solution.judgePoint24([3, 3, 8, 8]))  # 8/(3-8/3)=24

    def test_all_zeros(self):
        # 测试所有卡片都是0的情况
        self.assertFalse(self.solution.judgePoint24([0, 0, 0, 0]))

    def test_simple_addition(self):
        # 测试简单加法情况
        self.assertTrue(self.solution.judgePoint24([6, 6, 6, 6]))  # 6+6+6+6=24

    def test_low_values_cannot_reach_24(self):
        # 测试最小值组合无法达到24的情况
        self.assertFalse(self.solution.judgePoint24([1, 1, 1, 1]))

    def test_high_values_cannot_reach_24(self):
        # 测试较大值组合无法达到24的情况
        self.assertFalse(self.solution.judgePoint24([9, 9, 9, 9]))


if __name__ == '__main__':
    unittest.main()
